'''
Created on 15-07-2012

@author: z0nk
'''
import urllib2
import urllib
import cookielib
import re

class AuctionProvider(object):
    '''
    classdocs
    '''

    default_headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 6.2; rv:9.0.1) Gecko/20100101 Firefox/12.0.1",
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
        "Accept-Language": "en-us,en;q=0.5",
        "Accept-Encoding": "gzip, deflate",
        "Accept-Charset": "ISO-8859-1,utf-8;q=0.7,*;q=0.7",
        "Connection": "keep-alive"
    }

    def __init__(self):
        '''
        Constructor
        '''
        self.jar = cookielib.CookieJar()
        self.cookie = urllib2.HTTPCookieProcessor(self.jar)        
        self.opener = urllib2.build_opener(self.cookie)
        
        self.__zalogowany__ = False
    
    def openUrl(self,url,postdata="",headers=""):
        if headers == "" :
            tmpheaders = self.default_headers
        else:
            tmpheaders = self.default_headers
            tmpheaders.update(headers)
        
        params = urllib.urlencode(postdata)
        req = urllib2.Request(url, params,tmpheaders)
        res = self.opener.open(req)
        self.opener.close()
        return res
    
    def recognizeProvider(self, url):
        allegroregex='^(http(s)?:\/\/)(www\.)?allegro\.pl\/*'
        ebaygex='^(http(s)?:\/\/)(www\.)?ebay*'
        swistakregex='^(http(s)?:\/\/)(www\.)?swistak\.pl\/*'
        
        providers=(allegroregex,ebaygex,swistakregex)
        
        for i in range(len(providers)):
            regex = re.compile(providers[i])
            
            if(bool(regex.match(url))):
                return i+1
    

        
    